package com.ohuang.jiaxiaotong;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.example.myhttp.CallBackObject;
import com.example.myhttp.CallBackString;
import com.example.myhttp.Ihttp;
import com.ohuang.jiaxiaotong.Activity.LoginActivity;
import com.ohuang.jiaxiaotong.Util.Ohuang;
import com.ohuang.jiaxiaotong.entity.User;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.POST;
import retrofit2.http.QueryMap;

public class IndexActivity extends AppCompatActivity {
    private String name, password;

    private static final int SI_SUCCESS = 0, SI_FAIL = 1, SI_ERROR = 2, SI_OTHER = 3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);
        Ohuang.changeStatusBarTextImgColor(this, true);//将手机状态栏字体设置成黑色
        if (getSupportActionBar()!=null){
            getSupportActionBar().hide();
        }
        try {
            init();
        } catch (IOException e) {
            e.printStackTrace();
            Intent intent=new Intent(this,LoginActivity.class);
            startActivity(intent);
            finish();
        }
    }

    private void init() throws IOException {
        name = Ohuang.readfile(this, "username.txt");
        password = Ohuang.readfile(this, "password.txt");//读取保存的 用户名和密码
        if (name.isEmpty() || password.isEmpty()) {//如果没有用户名或密码的数据
            Intent intent = new Intent(this, LoginActivity.class);//进入登录Activity
            startActivity(intent);
            finish();
        } else {
            PostHttpRequest();//登录
        }
    }

    //登录
    private void PostHttpRequest() {
        HashMap<String,String> map=new HashMap<>();
        map.put("username", name);
        map.put("password", password);
        Ihttp.getInstance().post(MyHttpURL.mainUrl+"user/login",map,new CallBackString(){

            @Override
            public void success(String ojb) {
                if ("success".equals(ojb)) {
                    myHandler.sendEmptyMessage(SI_SUCCESS);
                } else if ("error".equals(ojb)) {
                    myHandler.sendEmptyMessage(SI_FAIL);
                } else {
                    myHandler.sendEmptyMessage(SI_OTHER);
                }
            }


            @Override
            public void fail(String s) {
                myHandler.sendEmptyMessage(SI_ERROR);
            }
        });

    }


    @SuppressLint("HandlerLeak")
    private Handler myHandler = new Handler() {

        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                case SI_SUCCESS:
                    HttpGetUser();
                    break;
                default:
                    Intent intent1 = new Intent(IndexActivity.this, LoginActivity.class);
                    startActivity(intent1);
                    finish();
                    break;
            }
        }
    };

    private void HttpGetUser() {
        HashMap<String, String> map = new HashMap<>();
        map.put("username", name);
        Ihttp.getInstance().post(MyHttpURL.mainUrl + "user/findUser", map, User.class, new CallBackObject<User>() {
            @Override
            public void success(User ojb) {
                User user = ojb;
                Intent intent = new Intent(IndexActivity.this, MainActivity.class);
                intent.putExtra("User", user);
                startActivity(intent);
                finish();
            }


            @Override
            public void fail(String s) {

            }
        });



    }



}
